﻿using System;
using System.Collections;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using WP7.InfoServiceRef;
using System.Collections.Generic;

namespace WP7
{
    public partial class Page2 : PhoneApplicationPage
    {
        System.Windows.Threading.DispatcherTimer dt = null;

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            init();
        }

        private void MyId(object sender, GetIDCompletedEventArgs id)
        {
            if (id.Error != null) { NavigationService.Navigate(new Uri("/../MainPage.xaml?error=dberror", UriKind.Relative)); }
            else
            {
                MainPage.MyIds = id.Result.First().ToString();
            }
        }

        private void change(object sender, GetLinesByTypeCompletedEventArgs e)
        {
            listBox1.ItemsSource = new List<int>();
            if (e.Error != null) { NavigationService.Navigate(new Uri("/../MainPage.xaml?error=dberror", UriKind.Relative)); }
            else
            {
                if (e.Result.Count == 0)
                {
                    textBlock2.Text = "Információ nem található";
                }
                else
                {
                    listBox1.ItemsSource = e.Result.ToList();
                }
            }
        }
        public Page2()
        {
            InitializeComponent();
        }

        private void init()
        {
            textBlock1.Text = "Hátralevő idő:";
            textBlock2.Text = "0 sec";

            textBlock5.Text = MainPage.gcw.Position.Location.Longitude.ToString();
            textBlock6.Text = MainPage.gcw.Position.Location.Latitude.ToString();

            dt = new System.Windows.Threading.DispatcherTimer();
            MainPage.sref.GetLinesByTypeCompleted += new EventHandler<GetLinesByTypeCompletedEventArgs>(change);
            MainPage.sref.getTimeFromStationCompleted += new EventHandler<getTimeFromStationCompletedEventArgs>(refreshLabel);
        }

        private void refreshLabel(object sender, getTimeFromStationCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                NavigationService.Navigate(new Uri("/../MainPage.xaml?error=dberror", UriKind.Relative));
            }
            textBlock2.Text = (e.Result/60).ToString() + " min " + (e.Result % 60).ToString() + " sec";
        }

        private void SelectedMachine(object sender, RoutedEventArgs e)
        {
            listBox1.Items.Clear();
                 if (radioButton1.IsChecked == true) { MainPage.type = "M"; }
            else if (radioButton2.IsChecked == true) { MainPage.type = "V"; }
            else if (radioButton3.IsChecked == true) { MainPage.type = "B"; }
            else if (radioButton4.IsChecked == true) { MainPage.type = "T"; }

            MainPage.sref.GetLinesByTypeAsync(MainPage.type);
        }

        private void refreshTime(object sender, EventArgs e)
        {
            
            float lat = (float)MainPage.gcw.Position.Location.Latitude;
            float lon = (float)MainPage.gcw.Position.Location.Longitude;

            textBlock5.Text = lon.ToString();
            textBlock6.Text = lat.ToString();

            if (listBox1.SelectedIndex >= 0) MainPage.sref.getTimeFromStationAsync((int)listBox1.SelectedItem, MainPage.type, lat, lon);
        }

        private void selected(object sender, SelectionChangedEventArgs e)
        {
            float lat = (float)MainPage.gcw.Position.Location.Latitude;
            float lon = (float)MainPage.gcw.Position.Location.Longitude;

            textBlock5.Text = lon.ToString();
            textBlock6.Text = lat.ToString();

            if(listBox1.SelectedIndex >= 0) MainPage.sref.getTimeFromStationAsync((int)listBox1.SelectedItem, MainPage.type, lat, lon);
            
            dt.Interval = new TimeSpan(0, 0, 30);
            dt.Tick += new EventHandler(refreshTime);
            dt.Start();
        }

        private void backToMenu(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/../MainPage.xaml", UriKind.Relative));
        }

        protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            dt.Stop();
            base.OnNavigatingFrom(e);
        }
    }
}